package com.maila.xiaoaoschool.fragment;


import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.activity.CollectionActivity;
import com.maila.xiaoaoschool.activity.ContactActivity;
import com.maila.xiaoaoschool.activity.DiscountActivity;
import com.maila.xiaoaoschool.activity.FeedbackActivity;
import com.maila.xiaoaoschool.activity.JoinActivity;
import com.maila.xiaoaoschool.activity.LoginActivity;
import com.maila.xiaoaoschool.activity.MessageActivity;
import com.maila.xiaoaoschool.activity.MyAddressActivity;
import com.maila.xiaoaoschool.activity.MyWalletActivity;
import com.maila.xiaoaoschool.activity.OrderActivity;
import com.maila.xiaoaoschool.activity.QuestionActivity;
import com.maila.xiaoaoschool.activity.RecommendActivity;
import com.maila.xiaoaoschool.activity.SettingActivity;
import com.maila.xiaoaoschool.api.LoginApi;
import com.maila.xiaoaoschool.entity.User;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.Constants;
import com.maila.xiaoaoschool.util.PrefUtil;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/9/23.
 */

public class PersonFragment extends Fragment {
    @BindView(R.id.ll_my_wallet)
    AutoLinearLayout llMyWallet;
    @BindView(R.id.imageView5)
    ImageView imageView5;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.ll_vip)
    AutoLinearLayout llVip;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.ll_quan)
    AutoLinearLayout llQuan;
    @BindView(R.id.ll_my_order)
    AutoLinearLayout llMyOrder;
    @BindView(R.id.all_address)
    AutoLinearLayout llAddress;
    @BindView(R.id.ll_collection)
    AutoLinearLayout llCollection;
    @BindView(R.id.ll_recommend)
    AutoLinearLayout llRecommend;
    @BindView(R.id.ll_kefu)
    AutoLinearLayout llKefu;
    @BindView(R.id.ll_feedback)
    AutoLinearLayout llFeedback;
    @BindView(R.id.ll_question)
    AutoLinearLayout llQuestion;
    @BindView(R.id.user_operation)
    AutoLinearLayout userOperation;
    @BindView(R.id.btn_shezhi)
    ImageView btnShezhi;
    @BindView(R.id.payment_iv)
    ImageView paymentIv;
    @BindView(R.id.order_count1)
    TextView orderCount1;
    @BindView(R.id.payment_rll)
    AutoRelativeLayout paymentRll;
    @BindView(R.id.user_icon)
    ImageView userIcon;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.agent_level_ll)
    AutoLinearLayout agentLevelLl;
    @BindView(R.id.user_background)
    AutoRelativeLayout userBackground;
    Unbinder unbinder;
    Gson gson = new Gson();

    public static PersonFragment newInstance(String param1) {
        PersonFragment fragment = new PersonFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_presonal, container, false);
        AutoUtils.autoSize(view);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initdata();
    }

    /**
     * 获取用户信息
     */
    private void initdata() {
        new LoginApi(getActivity(), new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                User user = gson.fromJson(result, User.class);
                if (user.getResult() == 1) {
                    if(user.getData().getLogo().contains("http:")){
                        Glide.with(getActivity()).load(Uri.parse(user.getData().getLogo())).into(userIcon);
                    }else {
                        Glide.with(getActivity()).load(Uri.parse(Constants.URL_ROOT_XIAOAO+user.getData().getLogo())).into(userIcon);
                    }
                  userName.setText(user.getData().getUsername());
                    PrefUtil.savePref(getActivity(),"is_login",1);
                } else if (user.getResult() == 3) {
                    userName.setText("登录/注册");
                    PrefUtil.savePref(getActivity(),"is_login",0);
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getuserinfo();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_my_wallet, R.id.ll_join,R.id.ll_vip, R.id.ll_quan, R.id.ll_my_order, R.id.all_address, R.id.ll_collection, R.id.ll_recommend, R.id.ll_kefu, R.id.ll_feedback, R.id.ll_question, R.id.btn_shezhi, R.id.payment_iv, R.id.agent_level_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_my_wallet:
                StartActivityUtils.start(getActivity(), MyWalletActivity.class);
                break;
            case R.id.ll_vip:
                break;
            case R.id.ll_quan:
                StartActivityUtils.start(getActivity(), DiscountActivity.class);
                break;
            case R.id.ll_my_order:
                StartActivityUtils.start(getActivity(), OrderActivity.class);
                break;
            case R.id.all_address:
                StartActivityUtils.start(getActivity(), MyAddressActivity.class);
                break;
            case R.id.ll_collection:
                StartActivityUtils.start(getActivity(), CollectionActivity.class);
                break;
            case R.id.ll_recommend:
                StartActivityUtils.start(getActivity(), RecommendActivity.class);
                break;
            case R.id.ll_kefu:
                StartActivityUtils.start(getActivity(), ContactActivity.class);
                break;
            case R.id.ll_feedback:
                StartActivityUtils.start(getActivity(), FeedbackActivity.class);
                break;
            case R.id.ll_question:
                StartActivityUtils.start(getActivity(), QuestionActivity.class);
                break;
            case R.id.btn_shezhi:
                StartActivityUtils.start(getActivity(), SettingActivity.class);
                break;
            case R.id.payment_iv:
                StartActivityUtils.start(getActivity(), MessageActivity.class);
                break;
            case R.id.agent_level_ll:
                if(PrefUtil.getIntPref(getActivity(),"is_login",-1)==1){

                }else {
                    StartActivityUtils.start(getActivity(),LoginActivity.class);
                }

                break;
            case R.id.ll_join:
                StartActivityUtils.start(getActivity(),JoinActivity.class);
                break;
        }
    }
}
